package com.sxr.spring;

import com.sxr.file.FileHandler;
import com.sxr.spring.file.DefaultFileHandler;
import com.sxr.integration.SecureFileService;
import com.sxr.integration.impl.DefaultSecureFileService;
import com.sxr.integration.tenant.TenantConfigProvider;
import com.sxr.servlet.ServletFileService;
import com.sxr.servlet.impl.DefaultServletFileService;
import com.sxr.servlet.util.ServletRequestParser;
import com.sxr.signature.VerifyService;
import com.sxr.signature.impl.RsaVerifyService;
import com.sxr.spring.tenant.SpringBootTenantConfigProvider;
import com.sxr.spring.web.SecureFileController;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * SXR文件签名自动配置
 *
 * @author SXR
 * @since 1.0.0
 */
@Configuration
@ConditionalOnProperty(prefix = "sxr.file-signature", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(SxrFileSignatureProperties.class)
public class SxrFileSignatureAutoConfiguration {

    /**
     * 签名服务Bean
     */
    @Bean
    @ConditionalOnMissingBean
    public VerifyService signatureService() {
        return new RsaVerifyService();
    }

    /**
     * 文件处理器Bean
     */
    @Bean
    @ConditionalOnMissingBean
    public FileHandler fileHandler(TenantConfigProvider tenantConfigProvider) {
        return new DefaultFileHandler(tenantConfigProvider);
    }

    /**
     * 租户配置提供者Bean
     * 使用Spring Boot配置属性提供者，支持从YAML配置文件读取租户信息
     */
    @Bean
    @ConditionalOnMissingBean
    public TenantConfigProvider tenantConfigProvider(SxrFileSignatureProperties properties) {
        return new SpringBootTenantConfigProvider(properties);
    }

    /**
     * 安全文件服务Bean
     */
    @Bean
    @ConditionalOnMissingBean
    public SecureFileService secureFileService(VerifyService verifyService, FileHandler fileHandler, TenantConfigProvider tenantConfigProvider) {
        return new DefaultSecureFileService(verifyService, fileHandler, tenantConfigProvider);
    }

    /**
     * Servlet文件服务Bean
     * 提供Servlet适配层的文件服务，支持配置化路径
     */
    @Bean
    @ConditionalOnMissingBean
    public ServletFileService servletFileService(SecureFileService secureFileService, SxrFileSignatureProperties properties) {
        // 创建路径配置适配器
        SxrFileSignatureProperties.PathConfig paths = properties.getPaths();
        ServletRequestParser requestParser = new ServletRequestParser( paths.getPreview() ,paths.getDownload() );
        return new DefaultServletFileService(secureFileService, requestParser);
    }

    /**
     * 安全文件控制器Bean
     */
    @Bean
    @ConditionalOnMissingBean
    public SecureFileController secureFileController(ServletFileService servletFileService) {
        return new SecureFileController(servletFileService);
    }
}
